from numpy import array

from libs.ZDisplay import ZDisplay


class ZAnimate ( ZDisplay ) :
    def __init__ ( self , father , image_list , time , loop = 0 , name = '' ) :
        self.image_list = array ( image_list )
        super ( ZAnimate , self ).__init__ ( father , self.image_list [ 0 ].get_rect ( ).size )
        self.father = father
        self.time = time
        self.loop = loop
        self.running = True
        self.over = False
        self.tick = 22 / 1000.00
        self.current = 0
        self.length = self.image_list.size
        self.current_time = .00
        self.current_round = 0
        self.name = name

    def play ( self ) :
        self.running = True
        self.over = False

    def stop ( self ) :
        self.running = False
        self.over = True

    def pause ( self ) :
        self.running = False
        self.over = False

    def draw ( self ) :
        if self.running :
            self.do_loop ( )
        image = self.image_list [ self.current ]
        self.father.image.fill ( 0 )
        self.father.image.blit ( image , self._pos )

    def do_time_loop ( self ) :
        if self.current_time > self.time :
            self.current_time = .00
            self.current += 1

    def do_round_loop ( self ) :
        if self.current < self.length - 1 :
            self.do_time_loop ( )
        else :
            if self.current_time > self.time :
                self.current_time = .00
                self.current = 0
                self.current_round += 1

    def do_loop ( self ) :
        self.current_time += self.tick
        if not self.loop :
            self.do_round_loop ( )
        else :
            if self.current_round < self.loop :
                self.do_round_loop ( )
            else :
                self.over = True

    def update ( self , *args ) :
        pass
